package bookManage;

import java.util.Random;

/**
 * 借阅卡 不可删除
 * 
 * @author 伟❤志
 *
 */
public class CardModel {

	private int id;// 借阅卡id-一经确定，不可修改
	private String password;// 密码

	public int getId() {
		return id;
	}

	public void setId(int id) {// id不可重复
		
		for (CardModel cardModel : Card.cards) {
			if (cardModel.getId() == id) {//找到相同id
				Random random = new Random();
				System.out.println("重复id！已为您自动生成新的id！");
				int newId = random.nextInt(2000000000) + 0;
				this.id = newId;
				return;
			}
		}
		
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 所有借阅卡
	 */
	public void selectAll() {
		for (CardModel cardModel : Card.cards) {
			cardInfo(cardModel);
		}
	}

	/**
	 * 根据id查借阅卡
	 * 
	 * id不可重复只需找到一个卡
	 * 
	 * @param id
	 * @return
	 */
	public CardModel selectById(int id) {
		for (CardModel cardModel : Card.cards) {
			if (cardModel.getId() == id) {
				return cardModel;
			}
		}
		return null;
	}

	/**
	 * 借阅卡详细信息
	 * 
	 * @param cardModel
	 */
	public void cardInfo(CardModel cardModel) {
		System.out.println("=================");
		System.out.println("借阅卡id：" + cardModel.getId());
		System.out.println("借阅卡密码：" + cardModel.getPassword());
		System.out.println("=================");
	}

	/**
	 * 注册 增加新的借阅卡
	 */
	public void addCard(int id, String password) {
		CardModel cardModel = new CardModel();

		cardModel.setId(id);
		cardModel.setPassword(password);

		Card.cards.add(cardModel);
	}

	/**
	 * 判断输入密码和原密码是否一致
	 * 
	 * @param cardModel
	 * @param password
	 * @return
	 */
	public boolean judge(CardModel cardModel, String password) {
		if (cardModel.getPassword().equals(password))
			return true;
		return false;
	}

	/**
	 * 修改密码
	 * 
	 * @param id
	 */
	public void updatePsw(int id, String password, String newPsw) {
		CardModel cardModel = selectById(id);

		if (cardModel == null) {
			System.out.println("没有找个这个借阅卡！");
			return;
		}

		boolean b = judge(cardModel, password);
		if (b) {// 原密码输入正确
			cardModel.setPassword(newPsw);
			System.out.println("修改成功！");
		}
	}
}
